package com.xuhuan.server.business.splitfile.controller;

import com.alibaba.fastjson2.JSONObject;
import com.xuhuan.server.business.splitfile.properties.FileSplitProperties;
import com.xuhuan.server.business.splitfile.service.IFileSplitFacadeService;
import com.xuhuan.server.business.splitfile.vo.ZoneMd5ListVO;
import com.xuhuan.server.business.splitfile.vo.ZoneCheckRequestVO;
import com.xuhuan.server.business.splitfile.vo.ZoneUploadVO;
import com.xuhuan.server.result.RequestResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author huan.xu
 * @Date 2023/6/16 16:12
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/server/split-file")
public class SplitFileController {

    @Autowired
    private IFileSplitFacadeService fileSplitFacadeService;

    /**
     * 获取参数配置
     *
     * @return
     */
    @PostMapping("/get-file-split-properties")
    public RequestResult getFileSplitProperties() {
        RequestResult requestResult = null;
        try {
            FileSplitProperties fileSplitProperties = fileSplitFacadeService.getFileSplitProperties();
            requestResult = RequestResult.success(fileSplitProperties);
        } catch (Exception e) {
            requestResult = RequestResult.fail(RequestResult.FAIL_CODE, e.getMessage());
            log.error("获取参数配置失败：{}", ExceptionUtils.getFullStackTrace(e));
        }
        return requestResult;
    }

    /**
     * 接收单个分片文件
     *
     * @param zoneUploadVO
     * @return
     */
    @PostMapping("/receive-sign-zone-file")
    public RequestResult receiveSignZoneFile(@RequestBody ZoneUploadVO zoneUploadVO) {
        log.info("访问方法[{}],分片唯一标识：{}", "receive-sign-zone-file", zoneUploadVO.getZoneMd5());
        RequestResult requestResult = null;
        try {
            fileSplitFacadeService.receiveSignZoneFile(zoneUploadVO);
            requestResult = RequestResult.success();
        } catch (Exception e) {
            requestResult = RequestResult.fail(RequestResult.FAIL_CODE, "服务端接收单个分片文件失败");
            log.error("服务端接收单个分片文件失败：{}", ExceptionUtils.getFullStackTrace(e));
        }
        return requestResult;
    }

    /**
     * 检查分片信息，返回协会现有的分片数据，下次就只需要再上传缺失的分片数据
     *
     * @param requestVO
     * @return
     */
    @PostMapping("/check-zone-file")
    public RequestResult<ZoneMd5ListVO> checkZoneFile(@RequestBody ZoneCheckRequestVO requestVO) {
        log.info("访问方法[{}],请求参数：{}", "check-zone-file", JSONObject.toJSONString(requestVO));
        RequestResult requestResult = null;
        try {
            ZoneMd5ListVO checkResult = fileSplitFacadeService.checkZoneFile(requestVO);
            requestResult = RequestResult.success(checkResult);
        } catch (Exception e) {
            requestResult = RequestResult.fail(RequestResult.FAIL_CODE, "服务端检查分片失败");
            log.error("服务端检查分片失败：{}", ExceptionUtils.getFullStackTrace(e));
        }
        return requestResult;
    }

    /**
     * 合并分片
     *
     * @param zoneMd5ListVO
     * @return
     */
    @PostMapping("/merge-zone-file")
    public RequestResult mergeZoneFile(@RequestBody ZoneMd5ListVO zoneMd5ListVO) {
        log.info("访问方法[{}],请求参数：{}", "merge-zone-file", JSONObject.toJSONString(zoneMd5ListVO));
        RequestResult requestResult = null;
        try {
            fileSplitFacadeService.mergeZoneFile(zoneMd5ListVO);
            requestResult = RequestResult.success();
        } catch (Exception e) {
            requestResult = RequestResult.fail(RequestResult.FAIL_CODE, "服务端合并分片失败");
            log.error("服务端合并分片失败：{}", ExceptionUtils.getFullStackTrace(e));
        }
        return requestResult;
    }

}
